/* Copyright  2009-2010
   @Author
   Richard Changde Yin            e-mail yinchangde@hotmail.com

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
#include<server_includes.h>
#include<debug.h>

int DebugDM_Alloc = 1;
int DebugDM_VarAlloc = 0;
int DebugDM_Lock = 0;
int DebugDM_Transaction = 0;
int DebugDM_UndoLog = 0;
int DebugDM_RedoLog = 0;
int DebugDM_Index = 0;
int DebugDM_HashIndex = 0;
int DebugDM_TreeIndex = 0;
int DebugDM_SystemDatabase = 0;
int DebugDM_Database = 0;
int DebugDM_Table = 0;
int DebugDM_Predicate = 0;
int DebugDM_TableIterator = 1;
int DebugDM_Process=0;
int DebugDM_Network=0;
int DebugDM_Gateway=0;
int DebugDM_Adapter=0;
int DebugDM_SqlLog=0;

#define COLLAPSAR_LOG_BUFFSIZE   2048

int printError1(BackEnd::DbRetVal, char* fname, int lno, char *format, ...)
{
  va_list ap;
  char mesgBuf[COLLAPSAR_LOG_BUFFSIZE];

  sprintf(mesgBuf, "%d:%lu:%s:%d:",
           OS::getpid(), OS::getthrid(), fname, lno);

  OS::write(2, mesgBuf, strlen(mesgBuf));

  va_start(ap, format);

  int err = ::vsnprintf(mesgBuf, sizeof(mesgBuf), format,ap);
  if(err < 0) {
      return err;
  }
  OS::write(2, mesgBuf, strlen(mesgBuf));
  strcpy(mesgBuf,"\n");
  OS::write(2, mesgBuf, strlen(mesgBuf));
   //2->stderr
  return 0;
}



int printDebug1(int module, char *fname, int lno, char *format, ...)
{
  switch(module) {
     case DM_Alloc: { if (!DebugDM_Alloc) return 1; break; }
     case DM_VarAlloc: { if (!DebugDM_VarAlloc) return 1; break; }
     case DM_Lock: { if (!DebugDM_Lock) return 1; break; }
     case DM_Transaction: { if (!DebugDM_Transaction) return 1; break; }
     case DM_UndoLog: { if (!DebugDM_UndoLog) return 1; break; }
     case DM_RedoLog: { if (!DebugDM_RedoLog) return 1; break; }
     case DM_HashIndex: { if (!DebugDM_HashIndex) return 1; break; }
     case DM_TreeIndex: { if (!DebugDM_TreeIndex) return 1; break; }
     case DM_SystemDatabase: { if (!DebugDM_SystemDatabase) return 1; break; }
     case DM_Database: { if (!DebugDM_Database) return 1; break; }
     case DM_Table: { if (!DebugDM_Table) return 1; break; }
     case DM_Iterator: { if (!DebugDM_TableIterator) return 1; break; }
     case DM_Predicate: { if (!DebugDM_Predicate) return 1; break; }
     case DM_Process: { if (!DebugDM_Process) return 1; break; }
     case DM_Network: { if (!DebugDM_Network) return 1; break; }
     case DM_Gateway: { if (!DebugDM_Gateway) return 1; break; }
     case DM_Adapter: { if (!DebugDM_Adapter) return 1; break; }
     case DM_SqlLog: { if (!DebugDM_SqlLog) return 1; break; }

  }

  va_list ap;

  char mesgBuf[COLLAPSAR_LOG_BUFFSIZE];

  sprintf(mesgBuf, "D:%s:%d:%lu:%s:%d:", moduleNames[module],
           OS::getpid(), OS::getthrid(), fname, lno);
  OS::write(1, mesgBuf, strlen(mesgBuf));

  va_start(ap, format);

  int err = ::vsnprintf(mesgBuf, sizeof(mesgBuf), format,ap);
  if(err < 0) {
      return err;
  }
  OS::write(1, mesgBuf, strlen(mesgBuf));
  strcpy(mesgBuf,"\n");
  OS::write(1, mesgBuf, strlen(mesgBuf));
   //1->stdout
  return 0;
}
